package com.wang.util;

import com.wang.thread.RollingWindowTimer;
import org.joda.time.DateTime;

/**
 * 滑动窗口算法
 * Created by wang on 2017/5/3.
 */
public class RollingWindow {
    //分段计算访问次数
    private static final int[] count = new int[6];

    public static void doCount(int whichSection){
        count[5]++;
    }

    //检查一分钟内是否超过流量限制
    public static boolean doCheck() {
        return (count[0]+count[1]+count[2]+count[3]+count[4]+count[5])<100;
    }

    //获取最近一分钟访问次数
    public static int getRequestTimes() {
        return (count[0]+count[1]+count[2]+count[3]+count[4]+count[5]);
    }

    public static int[] getCount() {
        return count;
    }

    //开启滑动窗口限流线程
    public static void startRollingWindowTime(){
        Thread thread = new Thread(new RollingWindowTimer(count));
        thread.start();
    }
}
